//
//  ViewController.swift
//  C150731_pdfViewer
//
//  Created by yao_yu on 15/7/31.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

class PDFView:UIView {
    var fileName:NSString = ""
    var pageNumber = 1
    
    
    override func drawRect(rect: CGRect) {
        let context:CGContextRef =  UIGraphicsGetCurrentContext();
        DisplayPDFPage(context, pageNumber: pageNumber, filename: fileName)
    }
    
    func getPDFDocumentRef(filename:NSString) -> CGPDFDocumentRef! {
        let path = CFStringCreateWithCString(kCFAllocatorDefault, filename.UTF8String, CFStringBuiltInEncodings.UTF8.rawValue)
        let url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, CFURLPathStyle.CFURLPOSIXPathStyle, 0)
        let document = CGPDFDocumentCreateWithURL(url)
        let count = CGPDFDocumentGetNumberOfPages(document)
        if count == 0 {
            print("[\(filename)]需要至少一页!")
            return nil
        } else {
            print("打开的PDF中有[\(count)]页!")
        }
        return document
    }
    
    func DisplayPDFPage(context:CGContextRef, pageNumber:Int, filename:NSString) {
        CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        let pdfDocument = getPDFDocumentRef(filename)
        let page = CGPDFDocumentGetPage(pdfDocument, pageNumber)
        CGContextSaveGState(context);
        let pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
        CGContextConcatCTM(context, pdfTransform);
        CGContextDrawPDFPage(context, page);
        CGContextRestoreGState(context);

    }
}

class ViewController: UIViewController {

//    var webView = UIWebView()
    var pdfView = PDFView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        pdfView.fileName = NSBundle.mainBundle().pathForResource("测试结果2.pdf", ofType: nil)!
        pdfView.backgroundColor = UIColor.clearColor()
        view.addSubview(pdfView)
//        view.addSubview(webView)
//        loadDocument()
    }
    
    
    
    func loadDocument() {
//        if let path = NSBundle.mainBundle().pathForResource("测试结果2.pdf", ofType: nil) {
//            _ = getPDFDocumentRef(path)
//            let url = NSURL(fileURLWithPath: path)
//            let request = NSURLRequest(URL: url)
//            webView.loadRequest(request)
//        }
    }

    override func viewDidLayoutSubviews() {
        pdfView.frame = CGRectMake(0, 20, view.frame.width, view.frame.height - 20)
        pdfView.setNeedsDisplay()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


   
//    
//    void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)
//    {
//    CGPDFDocumentRef document;
//    CGPDFPageRef page;
//    
//    document = GetPDFDocumentRef (filename);
//    page = CGPDFDocumentGetPage (document, pageNumber);
//    CGContextDrawPDFPage (myContext, page);
//    CGPDFDocumentRelease (document);
//    }
}

